package com.puman.watchtrade.fragment.personal;

import org.androidannotations.annotations.AfterInject;
import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Handler.Callback;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.animation.AlphaAnimation;
import android.view.animation.AnimationSet;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import cn.jpush.android.api.m;

import com.puman.watchtrade.MainActivity;
import com.puman.watchtrade.R;
import com.puman.watchtrade.fragment.LeftCategoryFragment;
import com.puman.watchtrade.fragment.info.InfoDetailFragment;
import com.puman.watchtrade.fragment.personal.handler.PersonalHttpHandler;
import com.puman.watchtrade.fragment.personal.model.RecordInfo;
import com.puman.watchtrade.fragment.set.AboutFragment;
import com.puman.watchtrade.fragment.set.LoginFragment;
import com.puman.watchtrade.fragment.set.httpHandler.SettingHttpHandler;
import com.puman.watchtrade.util.Config;
import com.puman.watchtrade.util.DataResult;
import com.puman.watchtrade.util.Gjfun;
import com.puman.watchtrade.util.GlobalData;
import com.puman.watchtrade.util.LoadingProgressDialog;

/*
 */
//@SuppressLint({ "ValidFragment", "NewApi" })
//@EFragment(R.layout.personal_center_layout)
//@EFragment(R.layout.personal_center_layout)
public class PersonalCenterFragment extends Fragment implements OnClickListener {

	/*
	 * ui
	 */
	// @ViewById(R.id.personal_bidding)
	TextView bidding;
	// @ViewById(R.id.personal_bidded)
	TextView bidded;
	// @ViewById(R.id.personal_unbidded)
	TextView unbidded;
	// @ViewById(R.id.personal_deal)
	TextView deal;
	// @ViewById(R.id.personal_login_name)
	TextView username;
	// @ViewById(R.id.personal_loginoutt)
	TextView loginOutBtn;

	public static final int LOGINOUT_SUC = 0x04;
	public static final int LOGINOUT_FAIL = 0x05;
	public static final int RECORD_SUC = 0x1;
	public static final int RECORD_FAIL = 0x2;

	private View mView;

	// boolean loadFlag = false;
	// public PersonalCenterFragment() {
	// // TODO Auto-generated constructor stub
	// }
	//
	// public PersonalCenterFragment(boolean loadFlag) {
	// this.loadFlag = loadFlag;
	// // TODO Auto-generated constructor stub
	// }

	// @AfterViews
	// void afterViews() {
	// initData();
	// }

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		mView = inflater.inflate(R.layout.personal_center_layout, container,
				false);
		initView();
		initData();
		loadAnimation();

		// GlobalData.userInfo.getType().equals("0");
		return mView;
	}

	ImageView certification;

	private void initView() {
		bidding = (TextView) mView.findViewById(R.id.personal_bidding);
		bidded = (TextView) mView.findViewById(R.id.personal_bidded);
		unbidded = (TextView) mView.findViewById(R.id.personal_unbidded);
		deal = (TextView) mView.findViewById(R.id.personal_deal);
		username = (TextView) mView.findViewById(R.id.personal_login_name);
		loginOutBtn = (TextView) mView.findViewById(R.id.personal_loginoutt);
		certification = (ImageView) mView.findViewById(R.id.certification);

		mView.findViewById(R.id.personal_item1).setOnClickListener(this);
		mView.findViewById(R.id.personal_item2).setOnClickListener(this);
		mView.findViewById(R.id.personal_item3).setOnClickListener(this);
		mView.findViewById(R.id.person_tobe_certification).setOnClickListener(
				this);
		mView.findViewById(R.id.personal_loginout_layout).setOnClickListener(
				this);
		mView.findViewById(R.id.main_left_imgbtn).setOnClickListener(this);

		mView.findViewById(R.id.bidding).setOnClickListener(this);
		mView.findViewById(R.id.bidded).setOnClickListener(this);
		mView.findViewById(R.id.unbidded).setOnClickListener(this);
		mView.findViewById(R.id.deal).setOnClickListener(this);

	}

	private void loadAnimation() {
		AlphaAnimation alphaAnimation = new AlphaAnimation(0, 1);
		// 设置动画执行的时间
		alphaAnimation.setDuration(1500);
		AnimationSet animationalpha = new AnimationSet(true);

		animationalpha.addAnimation(alphaAnimation);
		mView.findViewById(R.id.person_tobe_certification_info).startAnimation(
				animationalpha);
		mView.findViewById(R.id.personal_topBg).startAnimation(animationalpha);
		mView.findViewById(R.id.personal_mybidding_layout).startAnimation(
				animationalpha);
		mView.findViewById(R.id.personal_item).startAnimation(animationalpha);
		mView.findViewById(R.id.person_tobe_certification).startAnimation(
				animationalpha);
	}

	// @Click({ R.id.personal_item1, R.id.personal_item2, R.id.personal_item3,
	// R.id.person_tobe_certification, R.id.personal_loginout_layout,
	// R.id.main_left_imgbtn })
	public void onClick(View view) {
		FragmentTransaction transaction = MainActivity.fragmentManager
				.beginTransaction();
		switch (view.getId()) {
		case R.id.personal_loginout_layout:
			if (GlobalData.userInfo == null) {
				initData();
				LoginFragment loginFragment = new LoginFragment();
				transaction.replace(R.id.main_non_bottom, loginFragment);
				transaction.addToBackStack(null);
				transaction.commit();
			} else {
				// GlobalData.userInfo = null;
				// initData();
				loginOut();
			}
			break;
		case R.id.personal_item1:
			if (GlobalData.userInfo == null) {
				initData();
				LoginFragment loginFragment = new LoginFragment();
				transaction.replace(R.id.main_non_bottom, loginFragment);
				transaction.addToBackStack(null);
				transaction.commit();
			} else {
				BiddingRecordFregment biddingRecordFregment = new BiddingRecordFregment();
				transaction
						.replace(R.id.main_non_bottom, biddingRecordFregment);
				transaction.addToBackStack(null);
				transaction.commit();
			}
			break;
		case R.id.personal_item2:
			if (GlobalData.userInfo == null) {
				initData();
				LoginFragment loginFragment = new LoginFragment();
				transaction.replace(R.id.main_non_bottom, loginFragment);
				transaction.addToBackStack(null);
				transaction.commit();
			} else {
				SellRecordFregment sellRecordFregment = new SellRecordFregment();
				transaction.replace(R.id.main_non_bottom, sellRecordFregment);
				transaction.addToBackStack(null);
				transaction.commit();
			}
			break;
		case R.id.personal_item3:
			if (GlobalData.userInfo == null) {
				initData();
				LoginFragment loginFragment = new LoginFragment();
				transaction.replace(R.id.main_non_bottom, loginFragment);
				transaction.addToBackStack(null);
				transaction.commit();
			} else {
				SystemMsgFregment systemMsgFregment = new SystemMsgFregment();
				transaction.replace(R.id.main_non_bottom, systemMsgFregment);
				transaction.addToBackStack(null);
				transaction.commit();
			}
			break;
		case R.id.person_tobe_certification:
			// FragmentTransaction transaction =
			// MainActivity.fragmentManager.beginTransaction();
			AboutFragment aboutFragment = new AboutFragment("2");
			transaction.replace(R.id.main_non_bottom, aboutFragment);
			transaction.addToBackStack(null);
			transaction.commit();
			break;
		case R.id.main_left_imgbtn:
			MainActivity.getSlidingmunu().toggle();
			break;
		case R.id.bidding:
			if (!bidding.getText().toString().equals("0")) {
				if (GlobalData.userInfo == null) {
					initData();
					LoginFragment loginFragment = new LoginFragment();
					transaction.replace(R.id.main_non_bottom, loginFragment);
					transaction.addToBackStack(null);
					transaction.commit();
				} else {
					BiddingRecordFregment biddingRecordFregment = new BiddingRecordFregment(
							"1", "报价中");
					transaction.replace(R.id.main_non_bottom,
							biddingRecordFregment);
					transaction.addToBackStack(null);
					transaction.commit();
				}
			}
			break;
		case R.id.bidded:
			if (!bidded.getText().toString().equals("0")) {
				if (GlobalData.userInfo == null) {
					initData();
					LoginFragment loginFragment = new LoginFragment();
					transaction.replace(R.id.main_non_bottom, loginFragment);
					transaction.addToBackStack(null);
					transaction.commit();
				} else {
					BiddingRecordFregment biddingRecordFregment = new BiddingRecordFregment(
							"2", "已中标");
					transaction.replace(R.id.main_non_bottom,
							biddingRecordFregment);
					transaction.addToBackStack(null);
					transaction.commit();
				}
			}
			break;
		case R.id.unbidded:
			if (!unbidded.getText().toString().equals("0")) {
				if (GlobalData.userInfo == null) {
					initData();
					LoginFragment loginFragment = new LoginFragment();
					transaction.replace(R.id.main_non_bottom, loginFragment);
					transaction.addToBackStack(null);
					transaction.commit();
				} else {
					BiddingRecordFregment biddingRecordFregment = new BiddingRecordFregment(
							"3", "未中标");
					transaction.replace(R.id.main_non_bottom,
							biddingRecordFregment);
					transaction.addToBackStack(null);
					transaction.commit();
				}
			}
			break;
		case R.id.deal:
			if (!deal.getText().toString().equals("0")) {
				if (GlobalData.userInfo == null) {
					initData();
					LoginFragment loginFragment = new LoginFragment();
					transaction.replace(R.id.main_non_bottom, loginFragment);
					transaction.addToBackStack(null);
					transaction.commit();
				} else {
					BiddingRecordFregment biddingRecordFregment = new BiddingRecordFregment(
							"4", "成交");
					transaction.replace(R.id.main_non_bottom,
							biddingRecordFregment);
					transaction.addToBackStack(null);
					transaction.commit();
				}
			}
			break;
		default:
			break;
		}

	}

	private void initData() {
		if (GlobalData.userInfo == null) {
			bidded.setText("");
			bidding.setText("");
			unbidded.setText("");
			deal.setText("");
			username.setText("去登录");
			loginOutBtn.setText("未登录");
			certification.setVisibility(View.GONE);
		} else {
			// if (loadFlag) {
			// loadFlag = false;
			certification.setVisibility(View.VISIBLE);
			getData();
			// }else {
			bidding.setText(GlobalData.userInfo.getAuctioningCount());
			bidded.setText(GlobalData.userInfo.getWinCount());
			unbidded.setText(GlobalData.userInfo.getNotWinCount());
			deal.setText(GlobalData.userInfo.getTradedCount());
			// }
			username.setText(GlobalData.userInfo.getNick());
			loginOutBtn.setText("");
			Log.i("curry", "0:"+GlobalData.userInfo.getType());
			if (GlobalData.userInfo.getType().equals("0")) {
				Log.i("curry", "0:"+GlobalData.userInfo.getType());
				certification.setSelected(false);
			} else {
				Log.i("curry", "0:"+GlobalData.userInfo.getType());
				certification.setSelected(true);
			}

		}
	}

	private void getData() {
		mProgress = Gjfun.startProgressDialog(mProgress, getActivity());
		((MainActivity) getActivity()).cachedThreadPoolMinPriority
				.execute(recordRunnable);
	}

	/*
	 */
	private DataResult dataResult2 = new DataResult();
	private PersonalHttpHandler personalHttpHandler = new PersonalHttpHandler();
	Runnable recordRunnable = new Runnable() {
		@Override
		public void run() {
			int what = RECORD_FAIL;
			dataResult2 = personalHttpHandler.getRecord();
			if (dataResult2.flag) {
				what = RECORD_SUC;
			}
			sendMessage(what);
		}
	};

	private LoadingProgressDialog mProgress = null;

	private void loginOut() {
		mProgress = Gjfun.startProgressDialog(mProgress, getActivity());
		((MainActivity) getActivity()).cachedThreadPoolMinPriority
				.execute(loginOutRunnable);
	}

	private SettingHttpHandler settingHttpHandler = new SettingHttpHandler();
	private DataResult dataResult = new DataResult();
	/*
	 * 获取 商品详情
	 */
	Runnable loginOutRunnable = new Runnable() {
		@Override
		public void run() {
			int what = LOGINOUT_FAIL;
			dataResult = settingHttpHandler.loginOut();
			if (dataResult.flag) {
				what = LOGINOUT_SUC;
			}
			sendMessage(what);
		}
	};

	private void sendMessage(int what) {
		Message msg = new Message();
		msg.what = what;
		handler.sendMessage(msg);
	}

	Callback callback = new Callback() {
		@Override
		public boolean handleMessage(Message msg) {
			switch (msg.what) {
			case LOGINOUT_SUC:
				Gjfun.stopProgressDialog(mProgress);
				Toast.makeText(MainActivity.getInstant(), "" + dataResult.msg,
						Toast.LENGTH_SHORT).show();
				GlobalData.userInfo = null;
				initData();
				LeftCategoryFragment.userName.setText("未登录");
				LeftCategoryFragment.loginBtnInfo.setText("登录 ");
				break;
			case LOGINOUT_FAIL:
				Gjfun.stopProgressDialog(mProgress);
				Toast.makeText(MainActivity.getInstant(), "" + dataResult.msg,
						Toast.LENGTH_SHORT).show();
				break;
			case RECORD_SUC:
				Gjfun.stopProgressDialog(mProgress);
				RecordInfo recordInfo = new RecordInfo();
				recordInfo = (RecordInfo) dataResult2.object;
				bidding.setText(recordInfo.bidding);
				bidded.setText(recordInfo.bidded);
				unbidded.setText(recordInfo.unbidde);
				deal.setText(recordInfo.win);
				break;
			case RECORD_FAIL:
				break;
			default:
				break;
			}
			return false;
		}
	};
	Handler handler = new Handler(callback);
}
